package scripts.sea.await_sail.putstorage.pass.directsend.deliverGoods;

import api.sea.*;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;
import org.testng.collections.Lists;
import util.AssertUtil;
import util.HttpClientUtils;
import util.ResponseData;
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.*;

public class TestDeliverGoods {
    private String id;
    private String fileName;
    private String code;
    private static final Logger logger= LoggerFactory.getLogger(TestDeliverGoods.class);
    private static final StayPreplanApi stayPreplanApi=new StayPreplanApi();
    private final String number= RandomStringUtils.randomNumeric(8);
    private static final StuffApi stuffApi=new StuffApi();
    private static final StaySailApi staySailApi=new StaySailApi();
    private static final PutStorageApi putStorageApi=new PutStorageApi();
    private static final ArrivalPortApi arrivalPortApi=new ArrivalPortApi();
    private static final IcomeTestApi icomeTestApi=new IcomeTestApi();
    private static final AffixUploadApi affixUploadApi=new AffixUploadApi();
    @Test
    public void test_01() throws IOException, URISyntaxException {
        ResponseData responseData = LoginApi.login("admin", "qf123456?");
        String data = responseData.getBody().getJSONObject("data").getString("logistics-token");
        HttpClientUtils.build_header("logistics-token",data);
        //当使用文件上传和下载时  不使用此请求头
//        HttpClientUtils.build_header("Content-Type","application/json");
    }
    @Test
    public void test_02() throws IOException, URISyntaxException {
        JSONArray data=new JSONArray();
        Map<String,String> map_data=new HashMap<>();
        map_data.put("cartons","1");
        map_data.put("length","1");
        map_data.put("width","1");
        map_data.put("mark","Tokyo  Hot");
        map_data.put("weight","1");
        data.add(map_data);
        Date date=new Date();
        SimpleDateFormat time=new SimpleDateFormat("HH-mm-ss");
        String s=time.format(date);
        String date_time="QFHY-"+s;
        ResponseData responseData= DelegationApi.delegation("TCY179310074272899092","COOLPAI",""+date_time,
                0,"",data,"2021-03-22",
                "LIANYUNGANG","CHICAGO","pingming","2021-04-06","3",""+date_time,
                "1","LWL"+s,
                "100","1","0","","","","",
                1,1,1,
                "","","US","","","","","Tokyo  Hot","",
                "",""+date_time,
                "6","1","0",0,"王晓亮","5409d88922bf4d5187f4",
                "7cd6188ea979499e892e","黄德挺",
                "7cd6188ea979499e892e","黄德挺","创实",
                "CU163032594897092663",1);
        this.id=responseData.getBody().getJSONObject("data").getString("id");
        this.code=responseData.getBody().getJSONObject("data").getString("entrustmentCode");

    }
    @Test
    public void test_03() throws IOException, URISyntaxException {
        ResponseData responseData= stayPreplanApi.stay_preplan(code,id,number,1,"1.00","1.00","LIANYUNGANG",
                "CHICAGO","2","4","11","2021-04-14","2021-04-14",1);
        logger.info("responseData={}",responseData);
    }
    @Test
    public void test_04() throws IOException, URISyntaxException {
        ResponseData responseData=stuffApi.stuff(number,"1111111","111111","2021-04-09",1);
        logger.info("responseData={}",responseData);
        logger.info("number={}",number);
    }
    @Test
    public void test_05() throws IOException, URISyntaxException {
        String forward="huodaidanhao"+number;
        ResponseData responseData=staySailApi.stay_sail(number,forward,"isf","2021-04-09","xaingguan","denggfuan",1);
        logger.info("responseData={}",responseData);
        logger.info("number={}",number);
    }
    @Test
    public void test_06() throws IOException, URISyntaxException {
        ResponseData responseData= putStorageApi.put_storage("1",number,"2021-04-09","2021-04-09",1);
        logger.info("responseData={}",responseData);
        logger.info("number={}",number);
    }
    @Test
    public void test_07() throws IOException, URISyntaxException {
        ResponseData responseData= arrivalPortApi.arrival_port(id,number,"2021-04-08","2","qingguanhao","2021-04-08",0
                ,false,"1");
        logger.info("responseData={}",responseData);
    }
    @Test
    public void test_08() throws IOException, URISyntaxException {
        ResponseData responseData= icomeTestApi.icome_test(number,"1",2,"COOLPAI","TCY179310074272899092",
                "tihuodanhao","2021-04-08","2021-04-08",id,0);
    }
    @Test
    public void test_09() throws IOException, URISyntaxException {
        ResponseData responseData1 = LoginApi.login("admin", "qf123456?");
        String data = responseData1.getBody().getJSONObject("data").getString("logistics-token");
        HttpClientUtils.build_header("logistics-token",data);
        ResponseData responseData= affixUploadApi.affix_upload("test.png");
        logger.info("responseData={}",responseData);
        fileName=responseData.getBody().getString("data");
        logger.info("fileName={}",fileName);
    }
    @Test
    public void test_10() throws IOException, URISyntaxException {
        Map<String,String> data=new HashMap<>();
        data.put("fileName",fileName);
        data.put("name","test.png");
        List<?> list= Lists.newArrayList();
        net.sf.json.JSONArray jsonArray= net.sf.json.JSONArray.fromObject(data);
        String str=jsonArray.toString();
        logger.info("list={}",list);
        ResponseData responseData= DeliverGoodsApi.direct_goods(str,"2021-04-14","2021-04-14",1,
                "","","","",number,"1");
        AssertUtil.assertCommon(responseData,0,"添加成功");
    }
}
